/*
========================================================================
 Name        : CallDetailTrackerDeleteCallLogSelector.cpp
 Author      : ${author}
 Copyright   : ${copyright}
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <barsread.h>
#include <stringloader.h>
#include <eiklabel.h>
#include <eikenv.h>
#include <gdi.h>
#include <eikmfne.h>
#include <aknviewappui.h>
#include <eikappui.h>
#include <avkon.hrh>
#include <CallDetailTracker.rsg>
// ]]] end generated region [Generated System Includes]

// [[[ begin generated region: do not modify [Generated User Includes]
#include "CallDetailTrackerDeleteCallLogSelector.h"
#include "CallDetailTrackerDeleteCallLogSelectorView.h"
#include "CallDetailTracker.hrh"
#include "CallDetailTrackerMainForm.hrh"
#include "CallDetailTrackerViewReportOptionsForm.hrh"
#include "CallDetailTrackerGenerateReportForm.hrh"
#include "CallDetailTrackerProgressingForm.hrh"
#include "CallDetailTrackerDeleteCallLogSelector.hrh"
#include "CallDetailTrackerMainControler.hrh"
// ]]] end generated region [Generated User Includes]

#include "NamedDataBase.h"
// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * First phase of Symbian two-phase construction. Should not 
 * contain any code that could leave.
 */
CCallDetailTrackerDeleteCallLogSelector::CCallDetailTrackerDeleteCallLogSelector()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	iLbl_fromDate = NULL;
	iLbl_toDate = NULL;
	iDateEditor_fromDate = NULL;
	iDateEditor_toDate = NULL;
	iLbl_title = NULL;
	// ]]] end generated region [Generated Contents]
	
	}
/** 
 * Destroy child controls.
 */
CCallDetailTrackerDeleteCallLogSelector::~CCallDetailTrackerDeleteCallLogSelector()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	delete iLbl_fromDate;
	iLbl_fromDate = NULL;
	delete iLbl_toDate;
	iLbl_toDate = NULL;
	delete iDateEditor_fromDate;
	iDateEditor_fromDate = NULL;
	iControlEventDispatch.Close();		
	delete iDateEditor_toDate;
	iDateEditor_toDate = NULL;
	delete iLbl_title;
	iLbl_title = NULL;
	// ]]] end generated region [Generated Contents]
	
	}
				
/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aRect bounding rectangle
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return initialized instance of CCallDetailTrackerDeleteCallLogSelector
 */
CCallDetailTrackerDeleteCallLogSelector* CCallDetailTrackerDeleteCallLogSelector::NewL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CCallDetailTrackerDeleteCallLogSelector* self = CCallDetailTrackerDeleteCallLogSelector::NewLC( 
			aRect, 
			aParent, 
			aCommandObserver );
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aRect The rectangle for this window
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return new instance of CCallDetailTrackerDeleteCallLogSelector
 */
CCallDetailTrackerDeleteCallLogSelector* CCallDetailTrackerDeleteCallLogSelector::NewLC( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	CCallDetailTrackerDeleteCallLogSelector* self = new ( ELeave ) CCallDetailTrackerDeleteCallLogSelector();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
	}
			
/**
 * Construct the control (second phase).
 *  Creates a window to contain the controls and activates it.
 * @param aRect bounding rectangle
 * @param aCommandObserver command observer
 * @param aParent owning parent, or NULL
 */ 
void CCallDetailTrackerDeleteCallLogSelector::ConstructL( 
		const TRect& aRect, 
		const CCoeControl* aParent, 
		MEikCommandObserver* aCommandObserver )
	{
	if ( aParent == NULL )
	    {
		CreateWindowL();
	    }
	else
	    {
	    SetContainerWindowL( *aParent );
	    }
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
	InitializeControlsL();
	SetRect( aRect );
	ActivateL();
	// [[[ begin generated region: do not modify [Post-ActivateL initializations]
	// ]]] end generated region [Post-ActivateL initializations]
	
	}
			
/**
* Return the number of controls in the container (override)
* @return count
*/
TInt CCallDetailTrackerDeleteCallLogSelector::CountComponentControls() const
	{
	return ( int ) ELastControl;
	}
				
/**
* Get the control with the given index (override)
* @param aIndex Control index [0...n) (limited by #CountComponentControls)
* @return Pointer to control
*/
CCoeControl* CCallDetailTrackerDeleteCallLogSelector::ComponentControl( TInt aIndex ) const
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	switch ( aIndex )
		{
		case ELbl_fromDate:
			return iLbl_fromDate;
		case ELbl_toDate:
			return iLbl_toDate;
		case EDateEditor_fromDate:
			return iDateEditor_fromDate;
		case EDateEditor_toDate:
			return iDateEditor_toDate;
		case ELbl_title:
			return iLbl_title;
		}
	// ]]] end generated region [Generated Contents]
	
	// handle any user controls here...
	
	return NULL;
	}
				
/**
 *	Handle resizing of the container. This implementation will lay out
 *  full-sized controls like list boxes for any screen size, and will layout
 *  labels, editors, etc. to the size they were given in the UI designer.
 *  This code will need to be modified to adjust arbitrary controls to
 *  any screen size.
 */				
void CCallDetailTrackerDeleteCallLogSelector::SizeChanged()
	{
	CCoeControl::SizeChanged();
	LayoutControls();
	// [[[ begin generated region: do not modify [Generated Contents]
			
	// ]]] end generated region [Generated Contents]
	
	}
				
// [[[ begin generated function: do not modify
/**
 * Layout components as specified in the UI Designer
 */
void CCallDetailTrackerDeleteCallLogSelector::LayoutControls()
	{
	iLbl_fromDate->SetExtent( TPoint( -1, 72 ), TSize( 147, 29 ) );
	iLbl_toDate->SetExtent( TPoint( 0, 129 ), TSize( 146, 27 ) );
	iDateEditor_fromDate->SetExtent( TPoint( 157, 75 ), TSize( 81, 26 ) );
	iDateEditor_toDate->SetExtent( TPoint( 159, 130 ), TSize( 80, 25 ) );
	iLbl_title->SetExtent( TPoint( 8, 14 ), TSize( 227, 29 ) );
	}
// ]]] end generated function

/**
 *	Handle key events.
 */				
TKeyResponse CCallDetailTrackerDeleteCallLogSelector::OfferKeyEventL( 
		const TKeyEvent& aKeyEvent, 
		TEventCode aType )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	
	// ]]] end generated region [Generated Contents]
	
	if ( iFocusControl != NULL
		&& iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
		{
		return EKeyWasConsumed;
		}
	return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
	}

				
// [[[ begin generated function: do not modify
/**
 *	Initialize each control upon creation.
 */				
void CCallDetailTrackerDeleteCallLogSelector::InitializeControlsL()
	{
	iLbl_fromDate = new ( ELeave ) CEikLabel;
	iLbl_fromDate->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_CALL_DETAIL_TRACKER_DELETE_CALL_LOG_SELECTOR_LBL_FROM_DATE );
		iLbl_fromDate->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iLbl_toDate = new ( ELeave ) CEikLabel;
	iLbl_toDate->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_CALL_DETAIL_TRACKER_DELETE_CALL_LOG_SELECTOR_LBL_TO_DATE );
		iLbl_toDate->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iDateEditor_fromDate = new ( ELeave ) CEikDateEditor;
	iDateEditor_fromDate->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_CALL_DETAIL_TRACKER_DELETE_CALL_LOG_SELECTOR_DATE_EDITOR_FROM_DATE );
		iDateEditor_fromDate->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iDateEditor_fromDate->SetDate( TTime( TDateTime( 2010, EJuly, 0, 0, 0, 0, 0 ) ) );
	iDateEditor_fromDate->SetObserver( this );
	AddControlEventHandlerL( 
			iDateEditor_fromDate, 
			EEventRequestFocus, 
			&CCallDetailTrackerDeleteCallLogSelector::HandleDateEditor_fromDateRequestingFocusL );
	iDateEditor_toDate = new ( ELeave ) CEikDateEditor;
	iDateEditor_toDate->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_CALL_DETAIL_TRACKER_DELETE_CALL_LOG_SELECTOR_DATE_EDITOR_TO_DATE );
		iDateEditor_toDate->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	iDateEditor_toDate->SetDate( TTime( TDateTime( 2010, EAugust, 0, 0, 0, 0, 0 ) ) );
	iDateEditor_toDate->SetObserver( this );
	AddControlEventHandlerL( 
			iDateEditor_toDate, 
			EEventRequestFocus, 
			&CCallDetailTrackerDeleteCallLogSelector::HandleDateEditor_toDateRequestingFocusL );
	iLbl_title = new ( ELeave ) CEikLabel;
	iLbl_title->SetContainerWindowL( *this );
		{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_CALL_DETAIL_TRACKER_DELETE_CALL_LOG_SELECTOR_LBL_TITLE );
		iLbl_title->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
	
	iDateEditor_fromDate->SetFocus( ETrue );
	iFocusControl = iDateEditor_fromDate;
	
	}
// ]]] end generated function

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CCallDetailTrackerDeleteCallLogSelector::HandleResourceChange( TInt aType )
 {
 CCoeControl::HandleResourceChange( aType );
 SetRect(iAvkonViewAppUi->View(TUid::Uid(ECallDetailTrackerDeleteCallLogSelectorViewId))->ClientRect());
 // [[[ begin generated region: do not modify [Generated Contents]
 // ]]] end generated region [Generated Contents]
 
 }

void CCallDetailTrackerDeleteCallLogSelector::GetUserDate( )
	{
	 TBuf<20> startDate; //A text variable to store start date
	 TBuf<20> endDate;//A text variable to store end date
	   
	 startDate.Zero();//For some reason this is set to be zero
	 endDate.Zero();//For some reason this is set to be zero
	   
	 TDateTime Std;//DateTime object to get start date
	 TDateTime Etd;//DateTime object to get start date
	   
	 Std=iDateEditor_fromDate->Date().DateTime(); //Store the start date of the user
	 const TTime d = iDateEditor_fromDate->Date();

	 Etd=iDateEditor_toDate->Date().DateTime(); //Store the end date of the user
	   
	 startDate.Format(_L("%04d/%02d/%02d"), Std.Year(), Std.Month()+1, Std.Day()+1); //Convert the start date in to text
	 endDate.Format(_L("%04d/%02d/%02d"), Etd.Year(), Etd.Month()+1, Etd.Day()+1); //Convert the end date in to text 
	 	 
	 //startDate.Format(_L("%02d/%02d/%04d"), Std.Day()+1, Std.Month()+1, Std.Year()); //Convert the start date in to text
	 //endDate.Format(_L("%02d/%02d/%04d"), Etd.Day()+1, Etd.Month()+1, Etd.Year()); //Convert the end date in to text 
	 
	 //Send the details to the database object
	 CMyDBClass* iobjDB = new(ELeave) CMyDBClass;		 
	 iobjDB->DeleteFromDatabaseL(startDate, endDate);
	}

				
/**
 *	Draw container contents.
 */				
void CCallDetailTrackerDeleteCallLogSelector::Draw( const TRect& aRect ) const
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	CWindowGc& gc = SystemGc();
	gc.Clear( aRect );
	
	// ]]] end generated region [Generated Contents]
	
	}
				
/** 
 * Override of the HandleControlEventL virtual function
 */
void CCallDetailTrackerDeleteCallLogSelector::HandleControlEventL( 
		CCoeControl* aControl, 
		TCoeEvent anEventType )
	{
	for (int i = 0; i < iControlEventDispatch.Count(); i++)
		{
		const TControlEventDispatch& currEntry = iControlEventDispatch[i];
		if ( currEntry.src == aControl && currEntry.event == anEventType )
			{
			( this->*currEntry.handler )( aControl, anEventType );
			break;
			}
		}
	}
/** 
 * Helper function to register MCoeControlObserver event handlers
 */
void CCallDetailTrackerDeleteCallLogSelector::AddControlEventHandlerL( 
		CCoeControl* aControl, 
		TCoeEvent anEvent, 
		ControlEventHandler aHandler )
	{
	TControlEventDispatch entry;
	entry.src = aControl;
	entry.event = anEvent;
	entry.handler = aHandler;
	TInt err = iControlEventDispatch.Append( entry );
	User::LeaveIfError( err );
	}
				
/** 
 * Handle the EEventRequestFocus event.
 */
void CCallDetailTrackerDeleteCallLogSelector::HandleDateEditor_fromDateRequestingFocusL( CCoeControl* /* aControl */, TCoeEvent /* anEvent */ )
	{
	iDateEditor_fromDate->SetFocus( ETrue );
	iDateEditor_toDate->SetFocus( EFalse );
	iFocusControl = iDateEditor_fromDate;
	}
				
/** 
 * Handle the EEventRequestFocus event.
 */
void CCallDetailTrackerDeleteCallLogSelector::HandleDateEditor_toDateRequestingFocusL( CCoeControl* /* aControl */, TCoeEvent /* anEvent */ )
	{
	iDateEditor_fromDate->SetFocus( EFalse );
	iDateEditor_toDate->SetFocus( ETrue );
	iFocusControl = iDateEditor_toDate;
	}
